<?php
defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
//异常订单处理
class Controller_purchases_improper  extends Template
{
	public function before()
	{
		parent::before();
	}
	
	//不正常订单
	public function Action_index()
	{
		try {
			ACL::required('purchase improper');
		}
		catch (HTTP_Exception_403 $e )
		{
			$this->request->redirect(URL::base(true));
		}
		$this->title = __("异常采购单查询");
		$GET = array();
		$GET['purchaseorder_date_from'] = date('Y-m-d',strtotime('-7 days'));
		$GET['purchaseorder_date_to'] = date('Y-m-d',strtotime('now'));
		
		$purchaseorders_lists = NULL;
		if($_GET)
		{
			$GET = $_GET;//echo "2345";
		}
		
		//$purchaseorders_lists = $this->select($GET);
		if($this->select($GET))
		{
				$count = count($this->select($GET)->execute('newerp'));
				//var_dump($this->select($GET)->execute('newerp'));
				//分页
				$pagination         = Pagination::factory(array(
						'items_per_page'  =>5,
						'total_items'     => $count,
						'uri'	            => $this->request->uri(),
				));
				$purchaseorders_lists = $this->select($GET)
				->offset($pagination->_offset)
				->limit($pagination->items_per_page)
				->execute('newerp')
				->as_array();
		}
		
		$view = View::factory('purchase/improper');
		$view->set('GET',$GET);
		$view->set('purchaseorders_lists',$purchaseorders_lists);
		$view->set('pagination',$pagination);
		$this->response->body($view);
		
	}
	
	//查询采购单和详情信息
	protected function select(array $select_condition = NULL)
	{
		$purchase_lists = DB::select(array('purchaseorders.id','purchaseordersid'),'purchaseorders.*','purchaseorder_details.*')
		->from('purchaseorders')
		->join('purchaseorder_details')
		->on('purchaseorders.id','=','purchaseorder_details.purchaseorder_id')
		->where(DB::expr('purchaseorder_details.quantity - purchaseorder_details.warning_quantity*1.1'),'>',0)
		//->or_where(DB::expr('purchaseorder_details.quantity - purchaseorder_details.warning_quantity*0.9'),'<',0)
		->where(DB::expr('purchaseorder_details.prime_cost - purchaseorder_details.single_price'),'!=',0);
		
		if($select_condition)
		{
			//采购时间
			if(isset($select_condition['purchaseorder_date_from']))
			{
				$purchase_lists = $purchase_lists
				->where('purchaseorders.purchaseorder_date','>',$select_condition['purchaseorder_date_from']);
			}
			
	
			if(isset($select_condition['purchaseorder_date_to']))
			{
				$purchase_lists = $purchase_lists
				->where('purchaseorders.purchaseorder_date','<=',$select_condition['purchaseorder_date_to']);
			}
			
			if(isset($select_condition['purchaseordes_id']) and trim($select_condition['purchaseordes_id']))
			{
				$purchase_lists = $purchase_lists
				->where('purchaseorders.id','=',trim($select_condition['purchaseordes_id']));
			}
			//供应商名称
			if(isset($select_condition['supplier_name']) and trim($select_condition['supplier_name']))
			{
//              ----------------------------------kdw-------------------------------
//				$provider_id = DB::select(array('id','supplier_id'))
//				->from('erp_provider')
//				->where('erp_provider.vProviderName','LIKE','%'.trim($select_condition['supplier_name']).'%')
//				->execute('readonlyerp')
//				->as_array();

                $fields=array('id',);
                $supplier_name=trim($select_condition['supplier_name']);
                $condition="LIKE '%".$supplier_name."%'";
                $conditions=array('name'=>$condition);
                $provider_id=Newerpclientapi::getSuppliers($fields,$conditions);
//              -----------------------------------kdw-end---------------------------

				if($provider_id)
				{
					$supplier_ids = array();
					foreach ($provider_id as $supp)
					{
//                      ------------------kdw-----------------
//						$supplier_ids[] = $supp['supplier_id'];
                        $supplier_ids[] = $supp['id'];
//                      ------------------kdw-end-------------
					}
					//var_dump($supplier_ids);
					$purchase_lists = $purchase_lists
					->where('purchaseorders.supplier_id','in', array_values($supplier_ids));
				}
				else
				{
					return NULL;
				}
			}
			
			//采购员名称
			if(isset($select_condition['purchase_name']) and trim($select_condition['purchase_name']))
			{
				$purchase_id = ORM::factory('user')
				->where('name', 'LIKE', trim($select_condition['purchase_name']))
				->find();
				if($purchase_id->loaded())
				{
					$purchase_lists = $purchase_lists
					->where('purchaseorders.purchase_id','=',$purchase_id->id);
				}
				else
				{
					return NULL;
				}
			}
		}
		
		
		
		return $purchase_lists;
	}
}